package com.fr.report.parameter.processor;

import com.fr.base.FRContext;
import com.fr.base.XMLable;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.ScriptUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormulaUnitProcessor extends AbstractUnitProcessor
  implements XMLable
{
  private Formula formula;

  public FormulaUnitProcessor()
  {
    this(null);
  }

  public FormulaUnitProcessor(Formula paramFormula)
  {
    this.formula = null;
    this.formula = paramFormula;
  }

  public Object process(Map paramMap)
  {
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.pushNameSpace(ParameterMapNameSpace.create(paramMap));
    if (this.formula != null)
    {
      try
      {
        this.formula.setResult(ScriptUtils.executeFormula(localCalculator, this.formula, null, null, null));
      }
      catch (Throwable localThrowable)
      {
        FRContext.getLogger().log(Level.SEVERE, localThrowable.getMessage(), localThrowable);
      }
      return this.formula.getResult();
    }
    localCalculator.release();
    return null;
  }

  public Formula getFormula()
  {
    return this.formula;
  }

  public void setFormula(Formula paramFormula)
  {
    this.formula = paramFormula;
  }

  public Parameter[] getParameters()
  {
    return new Parameter[0];
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("Formula")))
    {
      this.formula = new Formula();
      paramXMLableReader.readXMLObject(this.formula);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (this.formula != null)
    {
      paramXMLPrintWriter.startTAG("Formula");
      this.formula.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
  }
}